/*

 * ServicioColor.java
 *
 * Created on __DATE__, __TIME__
 */

package cl.pelu.operacion;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListDataListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;


import cl.pelu.model.bo.dvo.FormulaBO;
import cl.pelu.model.bo.dvo.FormulaDVO;
import cl.pelu.model.bo.dvo.ServicioDetalleBO;
import cl.pelu.model.bo.dvo.TablaDVO;
import cl.pelu.utils.PeluFacade;



/**
 *
 * @author  __USER__
 */
public class ServicioColor extends javax.swing.JDialog {
	
	private javax.swing.JButton agregarButton;
	private javax.swing.JComboBox baseComboBox;
	private javax.swing.JLabel baseLabel;
	private javax.swing.JButton eliminarButton;
	private javax.swing.JTable formulaTable;
	private javax.swing.JComboBox gramosComboBox;
	private javax.swing.JLabel gramosLabel;
	private javax.swing.JComboBox gramosMatizComboBox;
	private javax.swing.JLabel gramosMatizLabel;
	private javax.swing.JButton guardarFormulaButton;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JComboBox matizComboBox;
	private javax.swing.JLabel matizLabel;
	private javax.swing.JLabel nombreServicioLabel;
	private javax.swing.JTextField nombreServicioTextField;
	private javax.swing.JComboBox oxidanteComboBox;
	private javax.swing.JLabel oxidanteLabel;
	private javax.swing.JLayeredPane servicioColorLayeredPane;
	private static ServicioColor myInstance;

	
	
	
	public static ServicioColor getInstance(java.awt.Frame parent, ServicioDetalleBO servicioDVO,	boolean modal) throws PropertyVetoException, SQLException 
	{
		
		if (myInstance == null) {
			myInstance = new ServicioColor(parent,servicioDVO,modal);
		}
		return myInstance;
	}

	public ServicioDetalleBO getServicioDVO() {
		return servicioDVO;
	}

	public void setServicioDVO(ServicioDetalleBO servicioDVO) {
		this.servicioDVO = servicioDVO;
	}

	private ServicioDetalleBO servicioDVO;

	
	public ServicioColor(java.awt.Frame parent, boolean modal) throws SQLException {
		super(parent, modal);
		initComponents();
	}
	
	public ServicioColor(java.awt.Frame parent, ServicioDetalleBO servicioDVO,	boolean modal) throws SQLException {
		super(parent, modal);
		initComponents();
		this.servicioDVO = servicioDVO;
		this.nombreServicioTextField.setText(servicioDVO.getNombre().getNombre());
		
		if(this.servicioDVO != null 
				&& servicioDVO.getFormula() != null 
				&& servicioDVO.getFormula().size() > 1 ){
			this.cargarTabla(servicioDVO.getFormula());
		}
		
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 * @throws SQLException 
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() throws SQLException {

		servicioColorLayeredPane = new javax.swing.JLayeredPane();
		nombreServicioTextField = new javax.swing.JTextField();
		nombreServicioLabel = new javax.swing.JLabel();
		baseLabel = new javax.swing.JLabel();
		gramosLabel = new javax.swing.JLabel();
		oxidanteLabel = new javax.swing.JLabel();	
		gramosComboBox = new javax.swing.JComboBox();
		baseComboBox = new javax.swing.JComboBox();
		oxidanteComboBox = new javax.swing.JComboBox();
		matizLabel = new javax.swing.JLabel();
		matizComboBox = new javax.swing.JComboBox();
		gramosMatizLabel = new javax.swing.JLabel();
		gramosMatizComboBox = new javax.swing.JComboBox();
		agregarButton = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		formulaTable = new javax.swing.JTable();
		guardarFormulaButton = new javax.swing.JButton();
		eliminarButton = new javax.swing.JButton();
		

		servicioColorLayeredPane.setBorder(javax.swing.BorderFactory.createTitledBorder(
				javax.swing.BorderFactory.createEtchedBorder(),"Servicio Color"));
		
		//labels
		nombreServicioLabel.setText("nombre servicio");
		nombreServicioLabel.setBounds(20, 30, 200, 20);
		servicioColorLayeredPane.add(nombreServicioLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
			
		baseLabel.setText("base");
		baseLabel.setBounds(150, 30, 200, 20);
		servicioColorLayeredPane.add(baseLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);

		gramosLabel.setText("gramos");
		gramosLabel.setBounds(300, 30, 200, 20);
		servicioColorLayeredPane.add(gramosLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		oxidanteLabel.setText("oxidante");
		oxidanteLabel.setBounds(450, 30, 200, 20);
		servicioColorLayeredPane.add(oxidanteLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		matizLabel.setText("matiz");
		matizLabel.setBounds(600, 30, 200, 20);
		servicioColorLayeredPane.add(matizLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		gramosMatizLabel.setText("gramos matiz");
		gramosMatizLabel.setBounds(750, 30, 200, 20);
		servicioColorLayeredPane.add(gramosMatizLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);

		

		//textfield
	
		nombreServicioTextField.setEditable(false);
		nombreServicioTextField.setText("tinte");
		nombreServicioTextField.setBounds(20, 70, 120, 20);

		baseComboBox =  new JComboBox( PeluFacade.getPeluqueriaComponent().getBases());
		baseComboBox.setBounds(150, 70, 120, 20);
		servicioColorLayeredPane.add(baseComboBox,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		gramosComboBox=  new JComboBox( PeluFacade.getPeluqueriaComponent().getGramos());
		gramosComboBox.setBounds(300, 70, 120, 20);
		servicioColorLayeredPane.add(gramosComboBox,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		oxidanteComboBox=  new JComboBox(  PeluFacade.getPeluqueriaComponent().getOxidante());
		oxidanteComboBox.setBounds(450, 70, 120, 20);
		servicioColorLayeredPane.add(oxidanteComboBox,javax.swing.JLayeredPane.DEFAULT_LAYER);


		matizComboBox=  new JComboBox( PeluFacade.getPeluqueriaComponent().getMatiz());
		matizComboBox.setBounds(600, 70, 120, 20);
		servicioColorLayeredPane.add(matizComboBox,javax.swing.JLayeredPane.DEFAULT_LAYER);

		gramosMatizComboBox=  new JComboBox(  PeluFacade.getPeluqueriaComponent().getGramosMatiz());
		gramosMatizComboBox.setBounds(750, 70, 120, 20);
		servicioColorLayeredPane.add(gramosMatizComboBox,javax.swing.JLayeredPane.DEFAULT_LAYER);

		agregarButton.setText("agregar");
		agregarButton.setBounds(900, 70, 120, 20);
		servicioColorLayeredPane.add(agregarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		agregarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				agregarButtonActionPerformed(evt);
			}
		});
		
		guardarFormulaButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				guardarFormulaButtonActionPerformed(evt);
			}
		});
		
		eliminarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				eliminaButtonPerformed(evt);
			}
		});


		
		servicioColorLayeredPane.add(nombreServicioTextField,javax.swing.JLayeredPane.DEFAULT_LAYER);

		
		formulaTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "base", "gramos", "oxidante", "matiz",
						"gramos matiz" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		
		
		jScrollPane1.setViewportView(formulaTable);

		guardarFormulaButton.setText("Guardar Formula");
		guardarFormulaButton.setActionCommand("guardarFomula");
		eliminarButton.setText("Eliminar");
		eliminarButton.setActionCommand("eliminar");
		

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap()
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										1003, Short.MAX_VALUE)
								.addGap(114, 114, 114))
				.addGroup(
						layout.createSequentialGroup()
								.addGap(20, 20, 20)
								.addComponent(eliminarButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										124,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addComponent(guardarFormulaButton)
								.addContainerGap(707, Short.MAX_VALUE))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(34, Short.MAX_VALUE)
								.addComponent(servicioColorLayeredPane,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										1083,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(servicioColorLayeredPane,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										131,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(48, 48, 48)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										108,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(	javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														guardarFormulaButton)
												.addComponent(eliminarButton)
														)));

		pack();
	}
	private void agregarButtonActionPerformed(java.awt.event.ActionEvent evt) {
		this.agregarDetalle();
	}

	private void guardarFormulaButtonActionPerformed(java.awt.event.ActionEvent evt) {

		
		if(formulaTable.getRowCount() > 0){
			recorrerTabla();
			this.dispose();
			
		}else{
			
			JOptionPane.showMessageDialog(null, "Debe ingresar almenos una formula");	
		}
		
		

	}

	void recorrerTabla() {

		

		for (int j = 0; j < formulaTable.getRowCount(); j++) {
		
			FormulaBO formulaBO = new FormulaBO();
			formulaBO.setBase((TablaDVO)formulaTable.getValueAt(j, 0));
			formulaBO.setGramos((TablaDVO)(formulaTable.getValueAt(j, 1)));
			formulaBO.setOxidante((TablaDVO)formulaTable.getValueAt(j, 2));
			formulaBO.setMatiz((TablaDVO)(formulaTable.getValueAt(j, 3)));
			formulaBO.setGramosMatiz((TablaDVO)(formulaTable.getValueAt(j, 4)));
			this.servicioDVO.addFormula(formulaBO);
		}

		

	}

		
	private void agregarDetalle() {

		DefaultTableModel tableModel = (DefaultTableModel) this.formulaTable.getModel();

		tableModel.addRow(new Object[] {(TablaDVO) baseComboBox.getSelectedItem(),
				(TablaDVO) gramosComboBox.getSelectedItem(),
				(TablaDVO) oxidanteComboBox.getSelectedItem(),
				(TablaDVO) matizComboBox.getSelectedItem(),
				(TablaDVO) gramosMatizComboBox.getSelectedItem()
	
		});

	}

	
	private void eliminaButtonPerformed(java.awt.event.ActionEvent evt) {
		DefaultTableModel tableModel = (DefaultTableModel) this.formulaTable.getModel();

		if (formulaTable.getSelectedRow() > -1) {
			tableModel.removeRow(formulaTable.getSelectedRow());
		}
	}
	
	
	
	private void cargarTabla(ArrayList<FormulaBO> formula) 
	{
		
		if(formula != null)
		{
			DefaultTableModel tableModel = (DefaultTableModel) this.formulaTable.getModel();
			for (FormulaBO formulaBO : formula) 
			{
				if(formulaBO != null){
					tableModel.addRow(new Object[] {(( TablaDVO)formulaBO.getBase()),
							(TablaDVO)formulaBO.getGramos(), 
							(TablaDVO) formulaBO.getOxidante(), 
							(TablaDVO) formulaBO.getMatiz(), 
							(TablaDVO) formulaBO.getGramosMatiz() 
				
					});
				}
			}
		}
	}


	public void limpiarFormulario() throws SQLException{
		
		baseComboBox.setSelectedIndex(0);
		gramosComboBox.setSelectedIndex(0);
		oxidanteComboBox.setSelectedIndex(0);
		matizComboBox.setSelectedIndex(0);
		gramosMatizComboBox.setSelectedIndex(0);
		limpiarTabla();
		 
	
	}

	void limpiarTabla (){
		
		DefaultTableModel tableModel = (DefaultTableModel) this.formulaTable.getModel();
		int a = tableModel.getRowCount();
		for (int i = 0; i < a; i++) {
			tableModel.removeRow(0);
			
		}
	}


}